package cn.sjx.send.controller;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.system.SystemUtil;
import cn.sjx.send.service.ISendService;
import cn.sjx.utils.RedisUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 发送 控制类
 *
 * @Author: sjx
 * @Date: 2019年09月05日 0005 10:02:03
 */
@Controller
@RequestMapping(value = "/")
public class SendController {

    private static final Log logger = LogFactory.get();

    /**
     * 获取当前主机IP地址
     */
    private static String IP_ADDRESS = StrUtil.replace(SystemUtil.getHostInfo().getAddress(), ".", "_");

    private static final String TIO_UDP_CLIENT_LINK_STATUS = "TIO_UDP_CLIENT:LINK_STATUS:{}";

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private ISendService sendService;

    /**
     * 跳转到测试也买你
     *
     * @return
     */
    @GetMapping
    public String bootjson(Model model) {
        Integer status = (Integer) redisUtils.get(StrUtil.format(TIO_UDP_CLIENT_LINK_STATUS, IP_ADDRESS));
        model.addAttribute("linkStatus", (status == null ? 0 : status));
        return "send/demo";
    }

    /**
     * 取消连接禁用
     *
     * @return
     */
    @GetMapping(value = "/reset")
    public String reset(Model model) {
        redisUtils.set(StrUtil.format(TIO_UDP_CLIENT_LINK_STATUS, IP_ADDRESS), 0);
        model.addAttribute("linkStatus", 0);
        return "send/demo";
    }

    /**
     * 启动udp客户端
     *
     * @param ip
     * @param port
     * @return
     */
    @ResponseBody
    @PostMapping(value = "startUdpClient")
    public JSONObject start(@RequestParam(required = true) String ip, @RequestParam(required = true) int port) {
        JSONObject jsonObject = new JSONObject();
        try {
            sendService.startUdpClient(ip, port);

            jsonObject.put("status", "SUCCESS");
            jsonObject.put("msg", "UDP客户端启动中...请查看控制台输出信息。");
        } catch (Exception e) {
            jsonObject.put("status", "ERROR");
            jsonObject.put("msg", e.getMessage());
        }
        return jsonObject;
    }

    /**
     * 发送数据
     *
     * @param hexData
     * @return
     */
    @ResponseBody
    @PostMapping(value = "sendHexData")
    public JSONObject sendHexData(@RequestParam(required = true) String hexData) {
        JSONObject jsonObject = new JSONObject();
        try {
            sendService.sendHexData(hexData);

            jsonObject.put("status", "SUCCESS");
            jsonObject.put("msg", "发送成功！");
        } catch (Exception e) {
            jsonObject.put("status", "ERROR");
            jsonObject.put("msg", e.getMessage());
        }
        return jsonObject;
    }

    /**
     * 批量测试
     *
     * @param model
     * @return
     */
    @GetMapping(value = "thread")
    public String thread(Model model) {
        final String[] resultHexDataArray = new String[]{
                "200E031300000000000000000000005DFB48F10000000100010701234C38B2000028E9",
                "200E031300000000000000000000005DFB48F10000000100010701234D44B200000CB9",
                "200E031300000000000000000000005DFB48F10000000100010701234C3EB200002861",
                "200E031300000000000000000000005DFB48F10000000100010701234C3FB20000299D",
                "200E031300000000000000000000005DFB48F10000000100010701234D3FB20000145D",
                "200E031300000000000000000000005DFB48F10000000100010701234C39B200002915",
                "200E031300000000000000000000005DFB48F10000000100010701234C42B2000031F1",
                "200E031300000000000000000000005DFB48F10000000100010701234C8BB200000E6D",
                "200E031300000000000000000000005DFB48F10000000100010701234C91B200000975",
                "200E031300000000000000000000005DFB48F10000000100010701234D42B200000C31",
                "200E031300000000000000000000005DFB48F10000000100010701234C85B200000C85",
                "200E031300000000000000000000005DFB48F10000000100010701234C82B200000DF1",
                "200E031300000000000000000000005DFB48F10000000100010701234C7DB200003DE5",
                "200E031300000000000000000000005DFB48F10000000100010701234C7EB200003DA1",
                "200E031300000000000000000000005DFB48F10000000100010701234C7CB200003C19",
                "200E031300000000000000000000005DFB48F10000000100010701234CEDB2000010E5",
                "200E031300000000000000000000005DFB48F10000000100010701234CE3B20000120D",
                "200E031300000000000000000000005DFB48F10000000100010701234C94B2000009B9",
                "200E031300000000000000000000005DFB48F10000000100010701234CE4B200001379",
                "200E031300000000000000000000005DFB48F10000000100010701234D36B2000017C1"
        };
        try {
            for (int i = 0; i < resultHexDataArray.length; i++) {
                final int j = i;
                ThreadUtil.excAsync(new Runnable() {
                    @Override
                    public void run() {
                        sendService.sendHexData(resultHexDataArray[j]);
                    }
                }, false);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        model.addAttribute("resultHexDataArray", resultHexDataArray);
        return "send/show";
    }
}
